package com.minimalist.basic.config.mybatis;

import cn.hutool.core.util.StrUtil;
import com.alibaba.druid.pool.DruidDataSource;
import com.minimalist.basic.config.mybatis.bo.BaseEntity;
import com.mybatisflex.codegen.Generator;
import com.mybatisflex.codegen.config.GlobalConfig;
import com.mybatisflex.codegen.config.TableConfig;
import com.mybatisflex.codegen.dialect.AbstractJdbcDialect;

/**
 * 配置Mybatis-Flex生成代码
 *
 * @author 11987
 */
public class GeneratorCodeHandler extends AbstractJdbcDialect {

    public static final String URL = "jdbc:dm://hcsdyyds.com:30236/MINIMALIST?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&nullCatalogMeansCurrent=true&clobAsString=1";

    public static final String USERNAME = "SYSDBA";

    public static final String PASSWORD = "85884288.aA@123";

    public static final String PACKAGE_PATH = "com.minimalist.basic";

    public static final Integer JDK_VERSION = 17;

    public static final String ENTITY_PACKAGE = "com.minimalist.basic.entity.po";

    public static final String[] IGNORE_COLUMNS = {"id", "deleted", "create_time", "create_id"
            , "update_id", "update_time", "version"};

    public static final String[] GENERATE_TABLES = {"M_USER_OPERATION"};

    public static final String MAPPER_PATH = "com.minimalist.basic.mapper";
//
//    public static final String MAPPER_TEMPLATE_PATH="template/mapper.ftl";
//
//    public static final String SERVICE_TEMPLATE_PATH="template/service.ftl";
//
//    public static final String SERVICE_IMPL_TEMPLATE_PATH="template/serviceImpl.ftl";
//
//    public static final String CONTROLLER_TEMPLATE_PATH="template/controller.ftl";

    @Override
    protected String forBuildColumnsSql(String schema, String tableName) {
        return "SELECT * FROM " + (StrUtil.isNotBlank(schema) ? schema + ".\"" : "")
                + tableName + "\" WHERE 1 = 2";
    }

    public static void main(String[] args) {
        //配置数据源
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl(URL);
        dataSource.setUsername(USERNAME);
        dataSource.setPassword(PASSWORD);
        //项目根目录
        String projectPath = System.getProperty("user.dir");
        //生成配置
        GlobalConfig globalConfig = new GlobalConfig();
        //设置根包
        globalConfig.setBasePackage(PACKAGE_PATH);
        //生成路径
        globalConfig.setSourceDir(projectPath + "\\generator\\java");

        /* ------------------------ 实体配置 ------------------------ */

        //生成实体
        globalConfig.setEntityGenerateEnable(true);
        //使用lombok
        globalConfig.setEntityWithLombok(true);
        //设置项目的JDK版本，项目的JDK为14及以上时建议设置该项，小于14则可以不设置
        globalConfig.setEntityJdkVersion(JDK_VERSION);
        //设置生成 entity 包名
        globalConfig.setEntityPackage(ENTITY_PACKAGE);
        //自动填充
        TableConfig tableConfig = new TableConfig();
        tableConfig.setInsertListenerClass(InsertFullColumnHandler.class);
        tableConfig.setUpdateListenerClass(UpdateFullColumnHandler.class);
        globalConfig.setTableConfig(tableConfig);
        //设置实体类的父类
        globalConfig.setEntitySuperClass(BaseEntity.class);
        //父类字段忽略
        globalConfig.getStrategyConfig().setIgnoreColumns(IGNORE_COLUMNS);

        /* ------------------------ mapper 配置 ------------------------ */

        //设置生成 mapper
        globalConfig.setMapperGenerateEnable(true);
        //mapper包
        globalConfig.setMapperPackage(MAPPER_PATH);
        //生成mapper xml文件
        globalConfig.enableMapperXml();
        //生成mapper.xml路径
        globalConfig.setMapperXmlPath(projectPath + "/generator/resources/mappers/evaluation");

        /* ------------------------ 生成哪些表 ------------------------ */
        globalConfig.setGenerateTable(GENERATE_TABLES);
        //是否生成service和controller
        globalConfig.setServiceGenerateEnable(true);
        globalConfig.setServiceImplGenerateEnable(true);
        globalConfig.setControllerGenerateEnable(true);

//        globalConfig.setMapperTemplatePath(MAPPER_TEMPLATE_PATH);
//        globalConfig.setServiceTemplatePath(SERVICE_TEMPLATE_PATH);
//        globalConfig.setServiceImplTemplatePath(SERVICE_IMPL_TEMPLATE_PATH);
//        globalConfig.setControllerTemplatePath(CONTROLLER_TEMPLATE_PATH);

        //通过 datasource 和 globalConfig 创建代码生成器
        Generator generatorHandler = new Generator(dataSource, globalConfig);
        generatorHandler.generate();
    }
}
